package com.fyy.im.server.handler;

import com.fyy.im.message.GQRequestMessage;
import com.fyy.im.message.GQResponseMessage;
import com.fyy.im.server.session.Group;
import com.fyy.im.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Author fyy
 * @Description
 * @Date 2021/5/12 15:29
 */
@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GQRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GQRequestMessage msg) throws Exception {
        Group group = GroupSessionFactory.getGroupSession().removeMember(msg.getGroupName(), msg.getUsername());
        if (group != null) {
            ctx.writeAndFlush(new GQResponseMessage(true, "已退出群" + msg.getGroupName()));
        } else {
            ctx.writeAndFlush(new GQResponseMessage(true, msg.getGroupName() + "群不存在"));
        }
    }
}
